package utils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by IntelliJ IDEA.
 * User: Stat
 * Date: 09.04.12
 * Time: 13:04
 * To change this template use File | Settings | File Templates.
 */
public class UserFileFactory {

    public static void copyFile(
            String source, String target)
            throws IOException
    {
        copyFile(new File(source),new File(target));
    }

    public static void copyFile(
            File source, File target)
            throws IOException
    {
        RandomAccessFile input=
                new RandomAccessFile(source,"r");
        RandomAccessFile output=
                new RandomAccessFile(target,"rw");
        try {
            byte[] buf = new byte[65536];
            long len= input.length();
            output.setLength(len);
            int bytesRead;
            while ((bytesRead=
                    input.read(buf,0,buf.length))>0)
                output.write(buf,0,bytesRead);
        } catch (IOException e) {
            try {input.close();} catch (Exception e1) {};
            try {output.close();} catch (Exception e1) {};
            return;
        }
        try {input.close();} catch (Exception e) {};
        output.close();
    }

}
